package com.sgb.kjwl.view.ui.adapter.self.order;

import android.annotation.SuppressLint;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ColorUtils;
import com.blankj.utilcode.util.SpanUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ItemOrderListBinding;
import com.sgb.kjwl.model.entity.user.OrderListEntity;
import com.sgb.kjwl.view.widget.RoundImageView;
import com.swgk.core.util.LogUtil;
import com.swgk.core.util.ResolutionUtil;

/**
 * 订单列表：8种
 * 采购订单：0待接单，1代发货，2待收货，3已完成
 * 销售订单：4待接单，5代发货，6待收货，7已完成
 * <p>
 * 2019-9-12 14：09：51
 * 需求新添加订单类型待确认：采购订单待确认：10，销售订单待确认14
 * <p>
 * <p>
 * if paymentMethod = 1 （线下付款）
 * 线下付款-待付款：status = 101
 * 线下付款-已付款-未确认收款：status  = 104
 * 线下付款-已确认收款-未发货：status  = 102
 * 线下付款-已发货-未收货：status  =103
 * 线下付款-已收货：status  =10l
 * else paymentMethod = 0 （线上付款）跟之前的逻辑一样，不用去改。
 * <p>
 * 注意：线下付款多了个104状态，此状态也属于履约中状态。
 *
 * ######特别说明：此商品订单适配器从1.4.7以后不再维护，新功能会在商品Module中进行扩展，慎复用#####
 */
public class OrderListAdapter extends BaseQuickAdapter<OrderListEntity, DataBindingViewHolder> {

    private final String type;
    private ItemOrderListBinding itemOrderList;

    public OrderListAdapter(String type) {
        super(R.layout.item_order_list);
        this.type = type;
    }


    @SuppressLint("SetTextI18n")
    @Override
    protected void convert(DataBindingViewHolder holder, OrderListEntity entity) {
        itemOrderList = (ItemOrderListBinding) holder.getBinding();
        itemOrderList.setViewModel(entity);
        RelativeLayout rl_order_record = holder.itemView.findViewById(R.id.rl_order_record);

        TextView tv_pay_info = holder.itemView.findViewById(R.id.tv_pay_info);
        TextView tv_deliver_info = holder.itemView.findViewById(R.id.tv_deliver_info);
        TextView tv_order_record = holder.itemView.findViewById(R.id.tv_order_record);
        TextView tv_status_pay = holder.itemView.findViewById(R.id.tv_status_pay);
        TextView tvContractInfo = holder.itemView.findViewById(R.id.tv_contract_info);

        TextView tvStatusOrder = holder.itemView.findViewById(R.id.tv_status_order);
        RoundImageView ivimgGoods = holder.itemView.findViewById(R.id.img_order_view);
        TextView tv_sku_list = holder.itemView.findViewById(R.id.tv_sku_list);
        TextView tv_goods_price = holder.itemView.findViewById(R.id.tv_goods_price);
        TextView tv_status_info = holder.itemView.findViewById(R.id.tv_status_info);
        TextView tv_order_person = holder.itemView.findViewById(R.id.tv_order_person);
        TextView tv_order_all_price = holder.itemView.findViewById(R.id.tv_order_all_price);
        TextView tv_other_cost = holder.itemView.findViewById(R.id.tv_other_cost);
        //取消订单按钮，暂时没用
        ImageView img_cancel_order = holder.itemView.findViewById(R.id.img_cancel_order);
        TextView tv_goods_count = holder.itemView.findViewById(R.id.tv_goods_count);
        TextView tv_title = holder.itemView.findViewById(R.id.tv_title);
        holder.itemView.findViewById(R.id.ziying_icon).setVisibility(entity.getSelfSupportTag() == 1 ? View.VISIBLE : View.GONE);
        tv_order_person.setText("负责人：" + entity.getPersonName());
        if ("1".equals(type)) {
            Drawable drawable = mContext.getResources().getDrawable(R.mipmap.arrow_right);
            //设置图片大小，必须设置
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            tv_title.setText(entity.getSupplierName());
            tv_title.setCompoundDrawables(null, null, drawable, null);
            tv_title.setCompoundDrawablePadding(ResolutionUtil.dp2px(mContext, 5));
            tv_order_person.setVisibility(View.VISIBLE);
        } else {
            Drawable drawable = mContext.getResources().getDrawable(R.mipmap.arrow_right);
            //设置图片大小，必须设置
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            tv_title.setText(entity.getOwnedName());
            tv_title.setCompoundDrawables(null, null, drawable, null);
            tv_title.setCompoundDrawablePadding(ResolutionUtil.dp2px(mContext, 5));
            tv_order_person.setVisibility(View.GONE);
        }

        //订单状态-1 待下单   0 待接单   101 待支付  102 待发货 103 已发货 7 已完成 8 已取消
        //采购订单
        if ("1".equals(type)) {
            switch (entity.getState()) {
                case -1:
                case 0:
                    tvStatusOrder.setText("待接单");
                    rl_order_record.setVisibility(View.GONE);
                    img_cancel_order.setVisibility(View.GONE);
                    tv_status_pay.setText("");
                    if (entity.getProcessType() != null && entity.placeProcessState != null) {
                        //采购商待接单下单审批中
                        if (entity.getProcessType() == 1 && entity.placeProcessState == 0) {
                            showOrGone(1, 0, 0);
                            tv_status_info.setText("");
                            itemOrderList.tvPoint.setVisibility(View.GONE);
                            //采购商待接单下单审批通过
                        } else if (entity.getProcessType() == 1 && entity.placeProcessState == 1) {
                            tv_status_info.setText("供应商接单中");
                            itemOrderList.tvPoint.setVisibility(View.VISIBLE);
                            showOrGone(0, 1, 0);
                            //采购商待接单下单审批拒绝
                        } else if (entity.getProcessType() == 1 && entity.placeProcessState == 2) {
                            tv_status_info.setText("");
                            itemOrderList.tvPoint.setVisibility(View.GONE);
                            showOrGone(0, 0, 1);
                        } else {
                            showOrGone(0, 0, 0);
                            itemOrderList.tvPoint.setVisibility(View.VISIBLE);
                            tv_status_info.setText("供应商接单中");
                        }
                    } else {
                        showOrGone(0, 0, 0);
                        itemOrderList.tvPoint.setVisibility(View.VISIBLE);
                        tv_status_info.setText("供应商接单中");
                    }
                    break;
                case 99:
                    tvStatusOrder.setText("待确认");
                    rl_order_record.setVisibility(View.VISIBLE);
                    img_cancel_order.setVisibility(View.GONE);
                    tvContractInfo.setVisibility(entity.contract ? View.VISIBLE : View.GONE);
                    tv_pay_info.setVisibility(View.GONE);
                    tv_deliver_info.setVisibility(View.GONE);
                    tv_order_record.setVisibility(View.GONE);
                    //待确认：供应商已接单
                    showOrGone(0, 0, 0);
                    itemOrderList.tvPoint.setVisibility(View.VISIBLE);
                    tv_status_info.setText("供应商已接单");
                    tv_status_pay.setText("");
                    break;
                case 101:
                case 102:
                case 103:
                    showOrGone(0, 0, 0);
                    tvStatusOrder.setText("履约中");
                    img_cancel_order.setVisibility(View.GONE);
                    rl_order_record.setVisibility(View.VISIBLE);
                    itemOrderList.tvPoint.setVisibility(View.VISIBLE);
                    tvContractInfo.setVisibility(entity.contract ? View.VISIBLE : View.GONE);
                    tv_order_record.setVisibility(View.VISIBLE);
                    initLatestDeliverState2(tv_status_info, tv_deliver_info, entity.deliverCount);
                    paymentMethod2(tv_status_pay, tv_pay_info, entity.payCount);
                    break;
                case 7:
                    tvStatusOrder.setText("已完成");
                    rl_order_record.setVisibility(View.VISIBLE);
                    img_cancel_order.setVisibility(View.GONE);
                    showOrGone(0, 0, 0);
                    itemOrderList.tvPoint.setVisibility(View.VISIBLE);
                    tvContractInfo.setVisibility(entity.contract ? View.VISIBLE : View.GONE);
                    tv_order_record.setVisibility(View.VISIBLE);
                    initLatestDeliverState2(tv_status_info, tv_deliver_info, entity.deliverCount);
                    paymentMethod2(tv_status_pay, tv_pay_info, entity.payCount);
                    break;
                case 8:
                    tvStatusOrder.setText("已取消");
                    tv_status_info.setText("");
                    itemOrderList.tvPoint.setVisibility(View.GONE);
                    rl_order_record.setVisibility(View.GONE);
                    img_cancel_order.setVisibility(View.GONE);
                    tv_status_pay.setText("");
                    showOrGone(0, 0, 0);
                    break;
                default:
                    tvStatusOrder.setText("");
                    tv_status_info.setText("");
                    itemOrderList.tvPoint.setVisibility(View.GONE);
                    rl_order_record.setVisibility(View.GONE);
                    img_cancel_order.setVisibility(View.GONE);
                    tv_status_pay.setText("");
                    showOrGone(0, 0, 0);
                    break;
            }
        } else {
            itemOrderList.approvalStatusOne.setVisibility(View.GONE);
            itemOrderList.approvalStatusTwo.setVisibility(View.GONE);
            itemOrderList.approvalStatusThree.setVisibility(View.GONE);
            //销售订单
            switch (entity.getState()) {
                case -1:
                case 0:
                    tvStatusOrder.setText("待接单");
                    rl_order_record.setVisibility(View.GONE);
                    img_cancel_order.setVisibility(View.GONE);
                    tv_status_info.setText("");
                    tv_status_pay.setText("");
                    itemOrderList.tvPoint.setVisibility(View.GONE);
                    // ProcessState 审批状态（0：待审批，1：审批通过 ，2审批不通过 3 无审批)
                    if (entity.getProcessType() != null && entity.getProcessState() != null) {
                        //供应商待接单审批中
                        if (entity.getProcessType() == 2 && entity.getProcessState() == 0) {
                            tv_status_info.setText("接单审批中");
                            itemOrderList.tvPoint.setVisibility(View.VISIBLE);
                        } else if (entity.getProcessType() == 2 && entity.getProcessState() == 1) {
                            tv_status_info.setText("接单审批已通过");
                            itemOrderList.tvPoint.setVisibility(View.VISIBLE);
                        } else if (entity.getProcessType() == 2 && entity.getProcessState() == 2) {
                            tv_status_info.setText("接单审批已拒绝");
                            itemOrderList.tvPoint.setVisibility(View.VISIBLE);
                        }
                    }
                    break;
                case 99:
                    tvStatusOrder.setText(0 != entity.contractStatus ? "合同洽谈中" : "等待采购商确认");
                    rl_order_record.setVisibility(View.VISIBLE);
                    img_cancel_order.setVisibility(View.GONE);
                    tvContractInfo.setVisibility(entity.contract ? View.VISIBLE : View.GONE);
                    tv_pay_info.setVisibility(View.GONE);
                    tv_deliver_info.setVisibility(View.GONE);
                    tv_order_record.setVisibility(View.GONE);
                    //待确认：供应商已接单
                    itemOrderList.tvPoint.setVisibility(View.GONE);
                    tv_status_info.setText("");
                    tv_status_pay.setText("");
                    if (entity.getProcessType() != null && entity.getProcessState() != null) {
                        //供应商待接单审批中
                        if (entity.getProcessType() == 2 && entity.getProcessState() == 0) {
                            tv_status_info.setText("接单审批中");
                            itemOrderList.tvPoint.setVisibility(View.VISIBLE);
                        } else if (entity.getProcessType() == 2 && entity.getProcessState() == 1) {
                            tv_status_info.setText("接单审批已通过");
                            itemOrderList.tvPoint.setVisibility(View.VISIBLE);
                        } else if (entity.getProcessType() == 2 && entity.getProcessState() == 2) {
                            tv_status_info.setText("接单审批已拒绝");
                            itemOrderList.tvPoint.setVisibility(View.VISIBLE);
                        }
                    }
                    break;
                case 101:
                case 102:
                case 103:
                    tvStatusOrder.setText(2 != entity.purchasingMethod ? "履约中" : "已签订合同");
                    tvContractInfo.setVisibility(entity.contract ? View.VISIBLE : View.GONE);
                    tv_order_record.setVisibility(View.VISIBLE);
                    img_cancel_order.setVisibility(View.GONE);
                    rl_order_record.setVisibility(View.VISIBLE);
                    itemOrderList.tvPoint.setVisibility(View.VISIBLE);
                    initLatestDeliverState2(tv_status_info, tv_deliver_info, entity.deliverCount);
                    paymentMethod2(tv_status_pay, tv_pay_info, entity.payCount);
                    break;
                case 7:
                    tvStatusOrder.setText("已完成");
                    rl_order_record.setVisibility(View.VISIBLE);
                    img_cancel_order.setVisibility(View.GONE);
                    itemOrderList.tvPoint.setVisibility(View.VISIBLE);
                    tvContractInfo.setVisibility(entity.contract ? View.VISIBLE : View.GONE);
                    tv_order_record.setVisibility(View.VISIBLE);
                    initLatestDeliverState2(tv_status_info, tv_deliver_info, entity.deliverCount);
                    paymentMethod2(tv_status_pay, tv_pay_info, entity.payCount);
                    break;
                case 8:
                    tvStatusOrder.setText("已取消");
                    tv_status_info.setText("");
                    itemOrderList.tvPoint.setVisibility(View.GONE);
                    rl_order_record.setVisibility(View.GONE);
                    img_cancel_order.setVisibility(View.GONE);
                    tv_status_pay.setText("");
                    break;
                default:
                    tvStatusOrder.setText("");
                    tv_status_info.setText("");
                    itemOrderList.tvPoint.setVisibility(View.GONE);
                    rl_order_record.setVisibility(View.GONE);
                    img_cancel_order.setVisibility(View.GONE);
                    tv_status_pay.setText("");
                    break;
            }
        }

        if (entity.getImgList() != null && entity.getImgList().size() > 0) {
            Glide.with(mContext).load(entity.getImgList().get(0)).error(R.mipmap.bg_default_placeholder).into(ivimgGoods);
        }
        StringBuilder buffer = new StringBuilder();
        for (int i = 0; i < entity.getSkuDetails().size(); i++) {
            if (entity.getSkuDetails().get(i).getName() != null && entity.getSkuDetails().get(i).getVal() != null) {
                buffer.append(entity.getSkuDetails().get(i).getName());
                buffer.append("：");
                buffer.append(entity.getSkuDetails().get(i).getVal());
                buffer.append("；");
            }
        }
        tv_sku_list.setText(buffer.toString());

        tv_goods_price.setText("¥" + entity.getMoney() + "/" + entity.getUnit());
        LogUtil.e("¥" + entity.getAmount());

        tv_order_all_price.setText("¥" + entity.getAmount());

        SpanUtils.with(tv_other_cost)
                .append("配送费")
                .setFontSize(13, true)
                .setForegroundColor(ColorUtils.getColor(R.color.color_969696))
                .appendSpace(15)
                .append("¥" + entity.getDeliveryCost())
                .setFontSize(12, true)
                .setForegroundColor(ColorUtils.getColor(R.color.color_333333))
                .appendSpace(30)
                .append("装卸费")
                .setFontSize(13, true)
                .setForegroundColor(ColorUtils.getColor(R.color.color_969696))
                .appendSpace(15)
                .append("¥" + entity.getHandlingCost())
                .setFontSize(12, true)
                .setForegroundColor(ColorUtils.getColor(R.color.color_333333))
                .appendSpace(30)
                .append("安装费")
                .setFontSize(13, true)
                .setForegroundColor(ColorUtils.getColor(R.color.color_969696))
                .appendSpace(15)
                .append("¥" + entity.getInstallationCost())
                .setFontSize(12, true)
                .setForegroundColor(ColorUtils.getColor(R.color.color_333333))
                .create();

        tv_goods_count.setText("x" + entity.getCount());

//        holder.setOnClickListener(R.id.img_cancel_order, v -> MToast.showToast(mContext, "要取消订单吗"));
//        holder.addOnClickListener(R.id.img_cancel_order);
        holder.addOnClickListener(R.id.tv_pay_info);
        holder.addOnClickListener(R.id.tv_deliver_info);
        holder.addOnClickListener(R.id.tv_order_record);
        holder.addOnClickListener(R.id.tv_title);
        holder.addOnClickListener(R.id.tv_contract_info);
    }

    private void showOrGone(int one, int two, int three) {
        //1显示 0 隐藏
        if (1 == one) {
            itemOrderList.approvalStatusOne.setVisibility(View.VISIBLE);
        } else {

            itemOrderList.approvalStatusOne.setVisibility(View.GONE);
        }
        if (1 == two) {
            itemOrderList.approvalStatusTwo.setVisibility(View.VISIBLE);
        } else {
            itemOrderList.approvalStatusTwo.setVisibility(View.GONE);
        }
        if (1 == three) {
            itemOrderList.approvalStatusThree.setVisibility(View.VISIBLE);
        } else {
            itemOrderList.approvalStatusThree.setVisibility(View.GONE);
        }
    }

    private void changeStatus(TextView tvStatus, String status, int statusColor) {
        SpanUtils.with(tvStatus)
                .append("付款进度：")
                .setForegroundColor(ColorUtils.getColor(R.color.color_333333))
                .append(status)
                .setForegroundColor(ColorUtils.getColor(statusColor))
                .create();
    }

    private void paymentMethod2(TextView tvStatus, TextView tvPayInfo, int payCount) {
        if(payCount >= 0) {
            changeStatus(tvStatus, "已付" + payCount + "笔", R.color.color_06C764);
            tvPayInfo.setVisibility(View.VISIBLE);
        } else {
            changeStatus(tvStatus, "已付0笔", R.color.color_main);
            tvPayInfo.setVisibility(View.GONE);
        }
    }

    private void initLatestDeliverState2(TextView tvStatus, TextView tvDeliverInfo, int deliverCount) {
        if(0 < deliverCount) {
            String deliverDesc = "已发货" + deliverCount + "单";
            tvStatus.setText(deliverDesc);
            tvDeliverInfo.setVisibility(View.VISIBLE);
        } else {
            tvStatus.setText("已发货0单");
            tvDeliverInfo.setVisibility(View.GONE);
        }
    }

}
